package com.webside.front.deputyindex.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.common.model.PBanner;
import com.webside.front.common.model.VideoFromWeb;
import com.webside.front.common.service.BSeriesService;
import com.webside.front.common.service.BVideoFromWebService;
import com.webside.front.common.service.BdictionaryService;
import com.webside.front.common.service.PBannerService;
import com.webside.front.listener.BaseData;
import com.webside.util.Common;

@Controller
@RequestMapping(value = "/info/")
public class InfoController {
	@Autowired
	BSeriesService bSeriesService;
	@Autowired
	BdictionaryService bdictionaryService;
	@Autowired
	PBannerService pBannerService;
	@Autowired
	BVideoFromWebService bVideoFromWebService;
	@RequestMapping("infoindex.html")
	public String getInfoIndex(Model m , HttpServletRequest request){
		/*
		 * 图片根路径
		 */
		m.addAttribute("baseImgPath",BaseData.img_base_path);
		
		/*
		 * 搜索关键字 
		 */
		m.addAttribute("keyWords",this.getSearchKeyWords());
		
		/*
		 * dictionary
		 */
		Map<String, Object> dictionary = new HashMap<String, Object>();
		
		/*
		 * 查字典表的类型
		 */
		dictionary.put("type", "DRAMA_TYPE");
		dictionary.put("code", "INFORMATION");
		dictionary.put("pageSize", 10);
		dictionary.put("sort", "sort");
		dictionary.put("orderType", "ASC");
		List<Bdictionary> dictionaryTypeList = bdictionaryService.selectBdictionaryByCondition(dictionary);
		m.addAttribute("dictionaryTypeList",dictionaryTypeList);

		/*
		 * 合作伙伴
		 */
		m.addAttribute("partnerList", this.getPartners());
		
		System.out.println(Common.BACKGROUND_PATH+"/front_first/videoIndex");
		return Common.BACKGROUND_PATH+"/front/deindex/infoIndex";
	}
	@RequestMapping(value="/find", method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String,Object> getVideoByType(String typeCode,Integer pageNo){
		/*
		 * 首页资讯
		 */
		Map<String, Object> infoMap = new HashMap<String, Object>();
		//公共条件
		infoMap.put("status", "PUBLISHED");
		infoMap.put("orderByColumn", "create_time");
		infoMap.put("orderByType", "DESC");
		infoMap.put("channel", "INFORMATION");
		infoMap.put("pageSize", 18);
		//传入类型
		infoMap.put("typeCode",typeCode);
		Integer pageNumber = (pageNo-1)*18;
		//根据页码定义第几条开始
		infoMap.put("pageNo", pageNumber);
		int count = bVideoFromWebService.selectCountByCondition(infoMap);
		List<VideoFromWeb> videoFromWeb = bVideoFromWebService.selectWebVideoByCondition(infoMap);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("videoFromWeb", videoFromWeb);
		map.put("videoCount",count);
		System.out.println(JSON.toJSON(map));
		return map;
	}
	
	
	
	/**
	 * 获取搜寻关键字
	 */
	private List<String> getSearchKeyWords(){
		Map<String, Object> keyWords = new HashMap<String, Object>();
		keyWords.put("type", "SEARCH_KEYWORDS");
		keyWords.put("code","SEARCH_KEYWORDS");
		List<Bdictionary> list = bdictionaryService.selectBdictionaryByCondition(keyWords);
		List<String> keyWord = new ArrayList<String>();
		String[] str = new String[]{};
		for (Bdictionary b : list) {
			str = b.getName().split(",");
			List<String> l = Arrays.asList(str);
			keyWord.addAll(l);
		}
		return keyWord;
	}
	/**
	 * 获取合作伙伴
	 * @return
	 */
	private List<PBanner> getPartners(){
		Map<String, Object> partnerMap = new HashMap<String, Object>();
		partnerMap.put("target", "PARTNER");
		partnerMap.put("orderColumn", "sort");
		partnerMap.put("orderType", "ASC");
		partnerMap.put("pageSize", 12);
		List<PBanner> partnerList = pBannerService.selectBannerByCondition(partnerMap);
		return partnerList;
	}
	
}
